package com.sx.ad.controller;

import com.alibaba.fastjson.JSON;
import com.sx.ad.annotation.IgnoreResponseAdvice;
import com.sx.ad.client.SponsorClient;
import com.sx.ad.client.vo.AdPlan;
import com.sx.ad.client.vo.AdPlanGetRequest;
import com.sx.ad.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author zkx
 * @Date 2024/9/2 18:58
 */
@Slf4j
@RestController
public class SearchController {

	private final RestTemplate restTemplate;

	private final SponsorClient sponsorClient;

	@Autowired
	public SearchController(RestTemplate restTemplate, SponsorClient sponsorClient) {
		this.restTemplate = restTemplate;
		this.sponsorClient = sponsorClient;
	}


	@IgnoreResponseAdvice
	@PostMapping("/getAdPlans")
	public CommonResponse<List<AdPlan>> getAdPlans(
			@RequestBody AdPlanGetRequest request
	) {
		log.info("ad-search: getAdPlans -> {}",
				JSON.toJSONString(request));
		return sponsorClient.getAdPlans(request);
	}

	@IgnoreResponseAdvice
	@PostMapping("/getAdPlansByRibbon")
	public CommonResponse<List<AdPlan>> getAdPlansByRebbon(@RequestBody AdPlanGetRequest request) {
		log.info("ad-search: getAdPlansByRibbon -> {}",
				JSON.toJSONString(request));
		return restTemplate.postForEntity(
				"http://eureka-client-ad-sponsor/ad-sponsor/get/adPlan",
				request,
				CommonResponse.class
		).getBody();
	}


}
